package com.central.scheduler.api.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.common.service.impl.SuperServiceImpl;
import com.central.scheduler.api.service.IMasterServerService;
import com.central.scheduler.dao.mapper.MasterServerMapper;
import com.central.scheduler.dao.model.MasterServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Tindy
 * @date 2021/11/21
 * @describe
 */
@Slf4j
@Service
public class MasterServerServiceImpl extends SuperServiceImpl<MasterServerMapper, MasterServer> implements IMasterServerService {

    @Override
    public PageResult<MasterServer> findMasters(Map<String, Object> params) {
        Page<MasterServer> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<MasterServer> list = baseMapper.findMasters(page, params);
        long total = page.getTotal();
        return PageResult.<MasterServer>builder().data(list).code(0).count(total).build();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result removeByIdAndOff(Long masterId) {
        MasterServer masterServer = baseMapper.selectById(masterId);
        if(masterServer.getStatus()==0){
            baseMapper.deleteById(masterId);
            return Result.succeed("操作成功");
        }else {
            return Result.succeed("操作失败，节点在线不可删除");
        }
    }
}
